<?
include_once("_class/config.php");
include_once("_class/Prodotto.class.php");
include_once("_class/Categoria.class.php");
include_once("_class/Carrello.class.php");
include_once("_class/Ordine.class.php");
include_once("_class/Configurazioni.class.php");
include_once("_class/MetodiPagamento.class.php");
include_once("_class/MetodiSpedizione.class.php");

include_once("_class/UploadImage.class.php");

class Negozio
{
	var $id;
	var $db;
	var $contesto = array();
	var $risorsa = array();
	var $cat_struct ;
	var $messaggio;
	var $carrello;
	var $ordine;
	var $pagamento;
	var $spedizione;
	var $configurazione;
	
	function Negozio($id_negozio, &$db)
	{
		$this->id = $id_negozio;
		$this->db = &$db;
		$this->configurazione = new Configurazioni($id_negozio, &$this->db);
		$this->pagamento = new MetodiPagamento($id_negozio, &$this->db, "", false);
		$this->spedizione = new MetodiSpedizione($id_negozio, &$this->db, "", false);
		
		
		$this->build_cat_struct();
		
		$this->carrello = new Carrello($id_negozio, &$this->db);		
		$this->ordine = new Ordine($id_negozio, &$this->carrello, &$db);
		
		
		/*
		$query = "SELECT 	c0.id as id_0, c0.nome_".$_SESSION[lingua]." as nome_0, c0.dir_".$_SESSION[lingua]." as dir_0, c0.ordine as ordine_0,
							c1.id as id_1, c1.nome_".$_SESSION[lingua]." as nome_1, c1.dir_".$_SESSION[lingua]." as dir_1, c1.ordine as ordine_1,
							c2.id as id_2, c2.nome_".$_SESSION[lingua]." as nome_2, c2.dir_".$_SESSION[lingua]." as dir_2, c2.ordine as ordine_2
					FROM moka_ecomm_$this->id.categorie as c0 
					LEFT JOIN moka_ecomm_$this->id.categorie as c1 ON c1.figlio_di = c0.id 
					LEFT JOIN moka_ecomm_$this->id.categorie as c2 ON c2.figlio_di = c1.id
					WHERE c0.figlio_di = 0 
					ORDER BY ordine_0, ordine_1, ordine_2";	
					//echo $query;
					//exit();
		$result = $this->db->query($query);
		if(mysql_num_rows($result) > 0)
		{
			while($row = mysql_fetch_assoc($result))
			{
				$tmp = array();
				$tmp[$row[id_0]] = array();
				$id_foglia = $row[id_0];
				//$this->cat_struct[cat_percorso][$row[id_0]][$row[id_0]] = array();
				//$this->cat_struct[cat_percorso][$row[id_0]][$row[id_0]] = array();
				//if(!is_array($this->cat_struct[albero][0][$row[id_0]]))$this->cat_struct[albero][0][$row[id_0]] = array();
				if($row[id_1]>0)
				{ 
					//$this->cat_struct[cat_percorso][$row[id_1]][$row[id_0]][$row[id_1]] = array();
					//$this->cat_struct[albero][0][$row[id_0]][$row[id_1]] = array();
				}
				if($row[id_2]>0) 
				{
					//$this->cat_struct[cat_percorso][$row[id_2]][$row[id_0]][$row[id_1]][$row[id_2]] = array();
					//$this->cat_struct[albero][0][$row[id_0]][$row[id_1]][$row[id_2]] = array();
				}
				for($i=0; $i<3; $i++)
				{
					if($row["id_$i"]>0)
					{
						$this->cat_struct[categorie][$row["id_$i"]]['nome']	=	$row["nome_$i"];
						$this->cat_struct[categorie][$row["id_$i"]]['dir']	=	$row["dir_$i"];
						$this->cat_struct[categorie][$row["id_$i"]]['ordine']	=	$row["ordine_$i"];
						
						//$this->cat_struct[categorie][$row["id_$i"]]['percorso']	=	$this->get_percorso( $this->cat_struct[cat_percorso][$row["id_0"]] , 0	);
						//print_r($this->cat_struct[cat_percorso][$row["id_0"]]);
						$this->cat_struct[categorie][$row["id_$i"]]['percorso']	=	$this->cat_struct[categorie][$row["id_0"]]['dir'];
						if($this->cat_struct[categorie][$row["id_1"]]['dir'])$this->cat_struct[categorie][$row["id_$i"]]['percorso']	.=	'/'.$this->cat_struct[categorie][$row["id_1"]]['dir'];
						if($this->cat_struct[categorie][$row["id_2"]]['dir'])$this->cat_struct[categorie][$row["id_$i"]]['percorso']	.=	'/'.$this->cat_struct[categorie][$row["id_2"]]['dir'];
					}				
				}
			}
			//print_r($this->cat_struct);
		}
*/
	}
	
	function build_cat_struct()
	{
		$this->cat_struct = array();
		$query = "SELECT id, nome_".$_SESSION[lingua]." as nome, dir_".$_SESSION[lingua]." as dir, ordine, figlio_di FROM moka_ecomm_$this->id.categorie 					
					ORDER BY ordine";	
					
		$result = $this->db->query($query);
		if(mysql_num_rows($result) > 0)
		{
			while($row = mysql_fetch_assoc($result))
			{
				foreach($row as $nom=>$val)$this->cat_struct[categorie][$row[id]][$nom]=$val;
			}
			$this->cat_struct[albero][0] = $this->albero($this->cat_struct[categorie]);
			//print_r($this->cat_struct[categorie]);
			$this->cat_struct[categorie] = $this->albero2d($this->cat_struct[albero][0]);
			//print_r($this->cat_struct[categorie]);
		}
		//print_r($this->albero2d($this->albero($this->cat_struct[categorie])));
		$GLOBALS[cat_struct] = $this->cat_struct;
	}

	function get_prodotto($file_prodotto, $percorso)
	{
		$prodotto = null;
		$query = "SELECT * FROM moka_ecomm_$this->id.prodotti WHERE file_".$_SESSION[lingua]."='$file_prodotto' ";	
		$result = $this->db->query($query);
		if(mysql_num_rows($result) > 0)
		{
			$row = mysql_fetch_object($result);
			$prodotto = new Prodotto($this->id, $row->id, &$this->db, $row);
			$tmpstr = $GLOBALS[cat_struct][categorie][$row->id_categoria][percorso_id];
			$tmpcatid = explode("/",substr($tmpstr,1));
			$tmpstr = $GLOBALS[cat_struct][categorie][$row->id_categoria][percorso];
			$tmpcat = explode("/",substr($tmpstr,1));
			//print_r($tmpcat);
			foreach($tmpcatid as $ncat=>$id_cat)$this->contesto[$id_cat] = $tmpcat[$ncat];
			
			$this->risorsa[$_REQUEST[prodotto]] = $prodotto->nome;
		}
		return $prodotto;
	}
	
	function get_categoria($percorso_o_id)
	{
		$categoria = null;
		
		if(!is_array($percorso_o_id))
		{
			$categoria = new Categoria($this->id, $percorso_o_id, &$this->db);
		}
		elseif(is_array($percorso_o_id))
		{
			$depth = count($percorso_o_id);
			foreach($percorso_o_id as $n=>$this_dir)
			{
				$spiece .= " c$n.id as id_$n, ";	
				if($n>0)$jpiece .= " LEFT JOIN moka_ecomm_$this->id.categorie as c$n ON c$n.figlio_di = c".($n-1).".id ";
				$wpiece .= " c$n.dir_".$_SESSION['lingua']."=\"$this_dir\" AND "; 
			}
			$spiece = substr($spiece,0,-2);
			$wpiece = substr($wpiece,0,-4);
			$query = "SELECT c".($depth-1).".id, $spiece FROM moka_ecomm_$this->id.categorie as c0 $jpiece WHERE $wpiece ";
			$result = $this->db->query($query);
			//echo $query;
			if(mysql_num_rows($result) > 0)
			{
				$row = mysql_fetch_object($result);
				$categoria = new Categoria($this->id, $row->id, &$this->db);
				for($i=0;$i<$depth;$i++)$this->contesto[$row->{'id_'.$i}] = $percorso_o_id[$i];
				
			}
		}

		return $categoria;
	}
	
	function get_home()
	{
		$home = new stdClass();
		$this->contesto[-1] = "home";
		return $home;
	}
	
	
	function get_id($host, &$db)
	{
		$id_negozio = 0;
		$query = "SELECT id FROM moka_ecommerce.negozi WHERE host='$host' OR host_test='$host' ";	
		$result = $db->query($query);
		if(mysql_num_rows($result) > 0)
		{
			$row = mysql_fetch_object($result);
			$id_negozio = $row->id;
		}
		return $id_negozio;
	}
	
	function get_percorso($id_categoria)
	{

		$percorso	=	$this->cat_struct[categorie][$id_categoria]['percorso'];
		
		 // es cat1/cat2;	
		 return $percorso;
	}
	
	function get_menubox_province($nome = 'provincia', $id = 'provincia', $selezione = '' )
	{
		$tmp = "<select name='$nome' id='$id'>\n";
		$tmp .= "<option value=\"\" ></option>\n";
		$query = "SELECT * FROM moka_ecommerce.province ORDER BY prov ";	
		$result = $this->db->query($query);
		while($row = mysql_fetch_object($result))
		{
			if($selezione==$row->prov)$sel="selected";
			else $sel = "";
			$tmp .= "<option value=\"".$row->prov."\" $sel >".$row->prov."</option>\n";
		}
		$tmp .= "</select>\n";
		return $tmp;
	}
	
	function get_varianti()
	{
		$query = "SELECT * FROM moka_ecomm_$this->id.varianti  ";	
		$result = $this->db->query($query);
		$varianti[0] = "Nessuna variante";
		while($row = mysql_fetch_object($result))
		{
			$varianti[$row->id][nome] = $row->nome_ita;
		}
		return $varianti;
	}
	
	
	function get_metodi_pagamento($idm="")
	{
		if($idm)$this->pagamento->setSel($idm);
		return $this->pagamento->get_metodi();
	}
	function get_metodi_spedizione($idm="")
	{
		
		if($idm)$this->spedizione->setSel($idm);
		return $this->spedizione->get_metodi();
	}
	
	function albero($lista, $root=0, $liv = -1,$albero = Array(), $percorso='', $percorso_id='')
	{
	       $liv ++ ;
	       foreach($lista as $id_p=>$tmp_val)
	       {
	               if($tmp_val[figlio_di]==$root)
	               {
	                       $tmp_val[liv] = $liv;
	                       $albero[figli][$id_p] = $tmp_val;
	                       $albero[figli][$id_p][percorso] = $percorso.'/'.$albero[figli][$id_p][dir];
	                       $albero[figli][$id_p][percorso_id] = $percorso_id.'/'.$id_p;
	                       $albero[figli][$id_p] = $this->albero($lista, $id_p, $liv ,$albero[figli][$id_p],$albero[figli][$id_p][percorso],$albero[figli][$id_p][percorso_id]);
	               }
	       }
	       $liv -- ;
	       return $albero;
	}

	function get_chart($tipo_grafico="ls", $argomento = 'fatturato' , $num_giorni=31, $w=640,$h=120 )
	{
		$data_inizio=mktime(0,0,0,date("n"),date("j")-$num_giorni,date("Y"));
		$data_fine=time();
		$query = "SELECT ordini.data_ins, ordini_righe.imponibile FROM moka_ecomm_$this->id.ordini INNER JOIN moka_ecomm_$this->id.ordini_righe WHERE ordini.data_ins>=$data_inizio AND ordini.data_ins<$data_fine ORDER BY ordini.data_ins DESC";	
		$result = $this->db->query($query);
		
		$data_tmp = array();
		$label_tmp = array();
		for($i=$num_giorni;$i>=0;$i--){
			$tmpt = mktime(0,0,0,date("n"),date("j")-$i,date("Y"));
			$data_tmp[$tmpt]=0;
			$label_tmp[$tmpt] = date("j",$tmpt);
		}
		while($row = mysql_fetch_object($result))
		{
			$day_time = mktime(0,0,0,date("n",$row->date_ins),date("j",$row->date_ins),date("Y",$row->date_ins));
			$data_tmp[$day_time] += round($row->imponibile) ;
			//echo "\nDT:$day_time IMP:$row->imponibile";
			
		}
		//print_r($data_tmp);
		//print_r($label_tmp);
		$chd = implode(",", $data_tmp);
		$chl = implode("|", $label_tmp); 
		$chds = "0,".max($data_tmp); // IMPOSTA SCALA VALORI
		$chxr = "1,0,".max($data_tmp); // asse (1:y 0:x), min, max IMPOSTA SCALA ASSI
		if($argomento=='fatturato')
		{
			$tmp_str = "http://chart.apis.google.com/chart?cht=".$tipo_grafico."&chs=".$w."x".$h."&chxt=x,y&chxr=$chxr&chds=$chds&chd=t:$chd&chl=$chl";
		}
		$tmp = "<img src=\"$tmp_str\" />";
		//echo $tmp_str;
		return $tmp;
	}
	
	function albero2d($albero, $a2d = Array())
	{
	       if(is_array($albero[figli]))foreach($albero[figli] as $id_f=>$tmp_val)
	       {

	               if(is_array($tmp_val[figli]))
	               {
	                       //$a2d[$id_f] = $tmp_val;
	                       foreach($tmp_val as $key=>$val)
	                       {
	                               if($key!="figli")$a2d[$id_f][$key] = $val;
	                       }
	                       $a2d = $this->albero2D($tmp_val, $a2d);
	               }else{
	                        $a2d[$id_f] = $tmp_val;
	               }
	
	       }
	       return $a2d;
	}
	
	function is_closed()
	{
		if($this->configurazione->get_param("chiuso")>0)return true;
		else return false;
	}
	
	
	
}?>
